import tkinter as tk
import time
tanqiu=tk.Tk()
tanqiu.title("弹球")
huabu=tk.Canvas(tanqiu,bg="blue",width=500,height=400)
huabu.pack()
xiaoqiu=huabu.create_oval(10,10,25,25,fill="red")
qiupai=huabu.create_rectangle(10,40,110,50,fill="red")
huabu.move(xiaoqiu,245,100)
huabu.move(qiupai,200,300)
y=-1
x=5
hitebottom=False
def qiudong():
    global y
    global x
    global hitebottom
    huabu.move(xiaoqiu,x,y)
    pos=huabu.coords(xiaoqiu)
    if pos[1]<=0:
        y=2
    if pos[3]>=400:
        hitebottom=True
    if pos[0]<=0:
        x=2
    if pos[2]>=500:
        x=-2
    if hit(xiaoqiu,qiupai)==True:
        y=-2
z=0
def paidong():
    global z
    huabu.move(qiupai,z,0)
    pos1=huabu.coords(qiupai)
    if pos1[0]<=0:
        z=0
    if pos1[2]>=500:
        z=0
def turnleft(event):
    global z 
    z=-2
def turnright(event):
    global z
    z=2
huabu.bind_all("<KeyPress-Left>",turnleft)
huabu.bind_all("<KeyPress-Right>",turnright)
def hit(xiaoqiu,qiupai):
    pos1=huabu.coords(qiupai)
    pos=huabu.coords(xiaoqiu)
    if pos[2]>=pos1[0] and pos[0]<=pos1[2] and pos[3]>=pos1[1] and pos[3]<=pos1[3]:
        return True
    else:
        return False
while True:
    if hitebottom==False:
        paidong()
        qiudong()
    else:
        huabu.create_text(250,150,text="游戏结束！",fill="red",font=("黑体",20,"bold"))
        print("游戏结束")
        break
    tanqiu.update()
    time.sleep(0.01)
tanqiu.mainloop()